﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Transactions;
using System.Xml.XPath;
using Songhay.Data.Common;
using Songhay.Data.GenericWeb.Properties;
using Songhay.Xml;

namespace Songhay.Data.GenericWeb.Procedure
{
    /// <summary>
    /// Insert procedures for Web-schema data.
    /// </summary>
    internal static partial class Insert
    {
        internal static XPathDocument Document(DbConnection commonConnection, IXPathNavigable navigableInput)
        {
            XPathNavigator node = XmlUtility.GetNavigableNode(navigableInput, "//FormData");
            XPathNavigator nodeDocumentSegments = XmlUtility.GetNavigableNode(navigableInput, "//DocumentSegments");
            XPathNavigator nodeDocumentKeywords = XmlUtility.GetNavigableNode(navigableInput, "//DocumentKeywords");

            Dictionary<string, object> paramSet = new Dictionary<string, object>();
            object p = null;
            XPathDocument d = null;

            p = XmlUtility.GetNodeValue(node, "./SegmentId", false, DBNull.Value);
            paramSet.Add("@SegmentId", p);

            p = XmlUtility.GetNodeValue(node, "./Title", true, DBNull.Value);
            paramSet.Add("@Title", p);

            p = XmlUtility.GetNodeValue(node, "./DocumentShortName", true, DBNull.Value);
            paramSet.Add("@DocumentShortName", p);

            p = XmlUtility.GetNodeValue(node, "./FileName", true, DBNull.Value);
            paramSet.Add("@FileName", p);

            p = XmlUtility.GetNodeValue(node, "./Path", true, DBNull.Value);
            paramSet.Add("@Path", p);

            p = XmlUtility.GetNodeValueAndParse<DateTime>(node, "./CreateDate", true, DateTime.Now);
            paramSet.Add("@CreateDate", p);

            p = XmlUtility.GetNodeValueAndParse<DateTime>(node, "./ModificationDate", true, DateTime.Now);
            paramSet.Add("@ModificationDate", p);

            p = XmlUtility.GetNodeValue(node, "./TemplateId", true, DBNull.Value);
            paramSet.Add("@TemplateId", p);

            p = XmlUtility.GetNodeValue(node, "./IsRoot", true, DBNull.Value);
            paramSet.Add("@IsRoot", p);

            p = XmlUtility.GetNodeValue(node, "./IsActive", true, DBNull.Value);
            paramSet.Add("@IsActive", p);

            p = XmlUtility.GetNodeValue(node, "./SortOrdinal", true, DBNull.Value);
            paramSet.Add("@SortOrdinal", p);

            p = XmlUtility.GetNodeValue(node, "./ClientId", true, DBNull.Value);
            paramSet.Add("@ClientId", p);

            p = XmlUtility.GetNodeValue(node, "./Tag", true, DBNull.Value);
            paramSet.Add("@Tag", p);

            using(TransactionScope tscope = new TransactionScope())
            {
                using(DbCommand cmd = commonConnection.CreateCommand())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = Resources.InsertDocument;

                    DbParameter[] paramArray = CommonParameter.GetParameters(cmd, paramSet);
                    cmd.Parameters.AddRange(paramArray);

                    p = cmd.ExecuteScalar();
                }

                if(nodeDocumentSegments != null)
                {
                    object segID = null; XPathNavigator segmentDocument = null;
                    foreach(IXPathNavigable n in nodeDocumentSegments.SelectChildren(XPathNodeType.Element))
                    {
                        segmentDocument = n.CreateNavigator();
                        segID = XmlUtility.GetNodeValue(segmentDocument, "./SegmentId", true, DBNull.Value);
                        Insert.SegmentDocument(commonConnection, segID, p);
                    }
                }

                if(nodeDocumentKeywords != null)
                {
                    object keywordValue = null; XPathNavigator documentKeyword = null;
                    foreach(IXPathNavigable n in nodeDocumentKeywords.SelectChildren(XPathNodeType.Element))
                    {
                        documentKeyword = n.CreateNavigator();
                        keywordValue = XmlUtility.GetNodeValue(documentKeyword, "./KeywordValue", true, DBNull.Value);
                        if(!string.IsNullOrEmpty(FrameworkType.ParseString(p)) && !string.IsNullOrEmpty(FrameworkType.ParseString(keywordValue)))
                        {
                            Insert.DocumentKeyword(commonConnection, p, keywordValue);
                        }
                    }
                }

                tscope.Complete();
            }

            d = Get.DocumentForEdit(commonConnection, p.ToString());

            return d;
        }
    }
}
